class MessageController {
	
	def parseService
	
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
    	 def chartData = ""
    	
         def runList = Run.list()
    	 chartData = parseService.buildChartData(runList)
    	 
        if(!params.max) params.max = 10
        [ messageInstanceList: Message.list( params ), chartData:chartData ]
    }

    def show = {
        def messageInstance = Message.get( params.id )

        if(!messageInstance) {
            flash.message = "Message not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ messageInstance : messageInstance ] }
    }

    def delete = {
        def messageInstance = Message.get( params.id )
        if(messageInstance) {
            messageInstance.delete()
            flash.message = "Message ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Message not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def messageInstance = Message.get( params.id )

        if(!messageInstance) {
            flash.message = "Message not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ messageInstance : messageInstance ]
        }
    }

    def update = {
        def messageInstance = Message.get( params.id )
        if(messageInstance) {
            messageInstance.properties = params
            if(!messageInstance.hasErrors() && messageInstance.save()) {
                flash.message = "Message ${params.id} updated"
                redirect(action:show,id:messageInstance.id)
            }
            else {
                render(view:'edit',model:[messageInstance:messageInstance])
            }
        }
        else {
            flash.message = "Message not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def messageInstance = new Message()
        messageInstance.properties = params
        return ['messageInstance':messageInstance]
    }

    def save = {
        def messageInstance = new Message(params)
        if(!messageInstance.hasErrors() && messageInstance.save()) {
            flash.message = "Message ${messageInstance.id} created"
            
            def runInstance = parseService.buildActivity(messageInstance.message)
            
            runInstance.save()
            
            
            redirect(action:list,id:messageInstance.id)
        }
        else {
            render(view:'create',model:[messageInstance:messageInstance])
        }
    }
    
    def saveRun = {
    	
    }
    
}
